package com.yanqu.road.dao.impl.cross.cricket;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.cricket.court.CricketCourtTopUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossCricketCourtTopUserDaoImpl extends TempDataObjectDao<CricketCourtTopUserData> {

    public Map<Integer, LinkedList<CricketCourtTopUserData>> getCricketCourtTopUserList(int round) {
        Map<Integer, LinkedList<CricketCourtTopUserData>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String selectSql = "select * from t_c_cricket_court_top_user_data where round = ? ";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setInt(1, round);
            rs = call.executeQuery();
            while (rs.next()) {
                CricketCourtTopUserData data = parse(rs);
                LinkedList<CricketCourtTopUserData> list = map.getOrDefault(data.getMatchGroupId(), new LinkedList<>());
                list.add(data);
                map.put(data.getMatchGroupId(), list);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String insertSql = "insert into `t_c_cricket_court_top_user_data`(serverId,userId,matchGroupId,round,score,param,rank) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CricketCourtTopUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<>();
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getMatchGroupId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, data.getParam()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getRank()));
        if (getDb().executeNoneQuery(insertSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    private static String updateSql = "update `t_c_cricket_court_top_user_data` set `matchGroupId`=?,`round`=?,`score`=?,`param`=?,`rank`=? where serverId=? and userId=? ";

    @Override
    protected boolean updateData(CricketCourtTopUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<>();
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getMatchGroupId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, data.getParam()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getRank()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CricketCourtTopUserData parseData(ResultSet rs) throws SQLException {
        CricketCourtTopUserData data = new CricketCourtTopUserData();
        data.setServerId(rs.getLong("serverId"));
        data.setUserId(rs.getLong("userId"));
        data.setMatchGroupId(rs.getInt("matchGroupId"));
        data.setRound(rs.getInt("round"));
        data.setScore(rs.getLong("score"));
        data.setParam(rs.getString("param"));
        data.setRank(rs.getInt("rank"));
        return data;
    }
}
